//
//  ZCSingerViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCSingerViewController.h"
#import <Masonry.h>
#import "ZCAFNetWork.h"
#import "ZCSigerModel.h"
#import "ZCSingerTableViewCell.h"
#import "ZCSingerContentViewController.h"
@interface ZCSingerViewController ()

@end

@implementation ZCSingerViewController

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    self.singerTableView.delegate = nil;
    self.singerTableView.dataSource = nil;
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _arrayOther = [[NSMutableArray alloc] init];
    _dataArray = [[NSMutableArray alloc] init];
    [self creatView];
    [self electricnetwork];
    [self electricenworkSinger];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
        make.width.equalTo(@100);
    }];
    
    [self.singerTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.view);
        make.left.equalTo(self.tableView.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    }];
    
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.right.equalTo(self.view).offset(0);
//        make.left.equalTo(self.tableView.mas_right).offset(0);
//        make.bottom.equalTo(self.view).offset(0);
        make.edges.equalTo(self.view).offset(0);
    }];
    // Do any additional setup after loading the view.
}

- (void)creatView
{
    self.imageView = [[UIImageView alloc] init];
    [self.imageView setImage:[UIImage imageNamed:@"3187"]];
    [self.view addSubview:self.imageView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.bounces = NO;
    [self.tableView setBackgroundColor:[UIColor colorWithRed:201/255.0 green:191/255.0 blue:148/255.0 alpha:1.0]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    self.singerTableView = [[UITableView alloc] initWithFrame:CGRectZero];
    [self.singerTableView setDelegate:self];
    [self.singerTableView setDataSource:self];
    self.singerTableView.emptyDataSetDelegate = self;
    self.singerTableView.emptyDataSetSource = self;
    [self.singerTableView setBackgroundColor:[UIColor clearColor]];
    self.singerTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.singerTableView.rowHeight = 80;
    [self.view addSubview:self.singerTableView];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _tableView) {
        static NSString * cellid = @"cell";
        UITableViewCell * cell = cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == NO) {
            cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
            [cell setBackgroundColor:[UIColor colorWithRed:181/255.0 green:163/255.0 blue:65/255.0 alpha:1.0]];
//            [cell setBackgroundColor:[UIColor clearColor]];
            cell.textLabel.numberOfLines = 0;
            ZCSigerModel * pageModel = [_arrayOther objectAtIndex:indexPath.row];
            cell.textLabel.text = pageModel.title;
        }
        return cell;
    }
    else if (tableView == _singerTableView) {
        static NSString * cellid = @"cell";
        ZCSingerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == NO) {
            cell = [[ZCSingerTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setBackgroundColor:[UIColor clearColor]];
        }
        if (_dataArray.count > 1) {
            ZCSigerModel * pageModel = [_dataArray objectAtIndex:indexPath.row];
            [cell getInfo:pageModel];
        }
        return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _tableView) {
        return [_arrayOther count];
    } else if (tableView == _singerTableView) {
        return _dataArray.count;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _tableView) {
        if (indexPath.row == 0) {
            [_dataArray removeAllObjects];
            [ZCAFNetWork getUrl:@"http://api.dongting.com/misc/singer/top" body:nil result:ZCJSON headerFile:nil success:^(id result) {
                if (result == nil) {
                    return;
                }else {
                NSMutableArray *array = [result objectForKey:@"data"];
                for (NSMutableDictionary *dic in array) {
                    ZCSigerModel *singer = [[ZCSigerModel alloc] init];
                    [singer setValuesForKeysWithDictionary:dic];
                    [_dataArray addObject:singer];
                    [_singerTableView reloadData];
                }
                }
            } failure:^(NSError *error) {
                
            }];
    } else if (indexPath.row == 1) {
            [_dataArray removeAllObjects];
            [ZCAFNetWork getUrl:@"http://api.dongting.com/misc/singer/hot" body:nil result:ZCJSON headerFile:nil success:^(id result) {
                if (result == nil) {
                    return;
                }else {
                NSMutableArray *array = [result objectForKey:@"data"];
                for (NSMutableDictionary *dic in array) {
                    ZCSigerModel *singer = [[ZCSigerModel alloc] init];
                    [singer setValuesForKeysWithDictionary:dic];
                    [_dataArray addObject:singer];
                    [_singerTableView reloadData];
                }
                }
            } failure:^(NSError *error) {
                
           }];
    } else {
        [_dataArray removeAllObjects];
        ZCSigerModel * pageModel = [_arrayOther objectAtIndex:indexPath.row];
        NSString *str = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=%@&size=1000&page=1&app=ttpod&v=v8.0.1.2015091618&uid=&mid=iPhone72C2&f=f320&s=s310&imsi=&hid=&splus=9.0.1&active=1&net=2&openudid=e65d9a0e3de1aa2b70ba313f1ca52326ce8e2e06&idfa=5B302CA5-E537-4B10-955D-520F4C91BAFE&utdid=VeQF2Frl2FrPcDAJS2FIF9m1eH3&alf=201200&bundle_id=com.ttpod.music&latitude=38.87981971999999&longtitude=121.54210854", pageModel.ID];
        [ZCAFNetWork getUrl:str body:nil result:ZCJSON headerFile:nil success:^(id result) {
            if (result == nil) {
                return;
            }else {
            NSMutableArray *array = [result objectForKey:@"data"];
            for (NSMutableDictionary *dic in array) {
                ZCSigerModel *singer = [[ZCSigerModel alloc] init];
                [singer setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:singer];
                [_singerTableView reloadData];
            }
            }
        } failure:^(NSError *error) {
            
        }];
}
    } else if (tableView == self.singerTableView) {
        ZCSingerContentViewController *next = [[ZCSingerContentViewController alloc] init];
        ZCSigerModel *model = [_dataArray objectAtIndex:indexPath.row];
        [next setModel:model];
        [self.navigationController pushViewController:next animated:YES];
    }
}

- (void)electricnetwork
{
    [ZCAFNetWork getUrl:@"http://api.dongting.com/misc/singer/top" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCSigerModel *singer = [[ZCSigerModel alloc] init];
            [singer setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:singer];
            [_singerTableView reloadData];
        }
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)electricenworkSinger
{
    [ZCAFNetWork getUrl:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5&f=f320&s=s310&imsi=&hid=&splus=8.0.2&active=1&net=2&openudid=ab660372b2d04c19e6fc28d765948acc67ea2508&idfa=BAFF6764-342C-47BF-BE94-D0A9ABA0AA76&utdid=VWhSy62rAGEDAEQ1pGShJ491&alf=201200&bundle_id=com.ttpod.music" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        if (result == nil) {
            return;
        }else {
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ZCSigerModel *singer = [[ZCSigerModel alloc] init];
            [singer setValuesForKeysWithDictionary:dic];
            [_arrayOther addObject:singer];
            [_tableView reloadData];
            singer.ID = [dic objectForKey:@"id"];
        }
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
